package com.athuiju.gateway.filtor;

import com.google.gson.JsonObject;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    private AntPathMatcher antPathMatcher=new AntPathMatcher();


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request= exchange.getRequest();
       String path=request.getURI().getPath();
        if (antPathMatcher.match("/eduservice/teacher/**",path)){
            List<String>tokenList=request.getHeaders().get("token");
            tokenList=new ArrayList<>();
            if (null==tokenList){
                ServerHttpResponse response=exchange.getResponse();
                return  out(response);
            }else {
                if (antPathMatcher.match("/eduservice/teacher/**",path)){
                    // List<String>tokenList=request.getHeaders().get("token");
                    if (null==tokenList||tokenList.size()==0){
                        ServerHttpResponse response=exchange.getResponse();
                        return  out(response);
                    }else {
                        ServerHttpResponse response=exchange.getResponse();
                        return  out(response);
                    }
                }
            }
        }
       //汇聚课堂api接口，校验用户必须登录
     /*   if (antPathMatcher.match("/api/**",path)){
            List<String>tokenList=request.getHeaders().get("token");
            if (null==tokenList){
                ServerHttpResponse response=exchange.getResponse();
                return  out(response);
            }else {
                ServerHttpResponse response=exchange.getResponse();
                return  out(response);
            }
        }*/
       if (antPathMatcher.match("/**/inner/**",path)){
           ServerHttpResponse response=exchange.getResponse();
           return  out(response);
       }
       return  chain.filter(exchange);
    }
    @Override
    public int getOrder(){
        return 0;
    }


    public Mono<Void> out(ServerHttpResponse response){
        JsonObject message=new JsonObject();
        message.addProperty("success",false);
        message.addProperty("code",28004);
        message.addProperty("data","鉴权失败");
        byte[]bits=message.toString().getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer=response.bufferFactory().wrap(bits);
        response.getHeaders().add("Content_Type","application/json;charset=UTF-8");
        return  response.writeWith(Mono.just(buffer));
    }
/*    @Override
    public Mono<Void>filter(ServerWebExchange exchange,GatewayFilterChain chain){
        ServerHttpRequest request=exchange.getRequest();
        String path =request.getURI().getPath();
        if (antPathMatcher.match("/eduservice/teacher/**",path)){
            List<String>tokenList=request.getHeaders().get("token");
            tokenList=new ArrayList<>();
            if (null==tokenList){
                ServerHttpResponse response=exchange.getResponse();
                return  out(response);
            }else {
                if (antPathMatcher.match("/eduservice/teacher/**",path)){
                   // List<String>tokenList=request.getHeaders().get("token");
                    if (null==tokenList||tokenList.size()==0){
                        ServerHttpResponse response=exchange.getResponse();
                        return  out(response);
                }else {
                    return  chain.filter(exchange);
                    }
                }
            }
        }
    }*/

}
